package models

import (
	"encoding/base64"
	"golang.org/x/crypto/scrypt"
	"sport_booking/dao"
	"sport_booking/utils/message"
)

type User struct {
	UUID        string `json:"uuid,omitempty" db:"user_uuid"`
	CreateStamp int64  `json:"user_create_stamp,omitempty" db:"user_create_stamp"`
	UpdateStamp int64  `json:"user_update_stamp,omitempty" db:"user_update_stamp"`
	Nick        string `json:"user_nick,omitempty" db:"user_nick"`
	Name        string `json:"user_name,omitempty" db:"user_name"`
	Birthday    string `json:"user_birthday,omitempty" db:"user_birthday"`
	Sex         string `json:"user_sex,omitempty" db:"user_sex"`
	Phone       string `json:"user_phone,omitempty" db:"user_phone"`
	Email       string `json:"user_email,omitempty" db:"user_email"`
	Password    string `json:"user_password,omitempty" db:"user_password"`
	Department  string `json:"user_department,omitempty" db:"user_department"`
	Job         string `json:"user_job,omitempty" db:"user_job"`
	Role        string `json:"user_role,omitempty" db:"user_role"`
	Code        int    `json:"user_code,omitempty"`
	Account     string `json:"user_account,omitempty"`
}

/*
密码加密
*/
func EncryptPassword(password string) (string, int) {
	const keyLen = 32
	salt := make([]byte, 8)
	salt = []byte{21, 43, 72, 32, 78, 23, 93, 54}
	// Key(password []byte, salt []byte, N int, r int, p int, keyLen int)
	hashPassword, err := scrypt.Key([]byte(password), salt, 16384, 8, 1, keyLen)
	if err != nil {
		return "", message.ErrorUserEncrypt
	}
	fpw := base64.StdEncoding.EncodeToString(hashPassword)
	return fpw, message.SUCCESS
}

/*
添加用户部分信息
*/
func AddUserInfo(user *User) int {
	sqlStr := "INSERT INTO bk_users (user_uuid,user_create_stamp,user_update_stamp,user_nick,user_email,user_password,user_role) VALUES (?,?,?,?,?,?,?)"
	result, err := dao.DB.Exec(sqlStr, user.UUID, user.CreateStamp, user.UpdateStamp, user.Nick, user.Email, user.Password, user.Role)
	if err != nil {
		return message.ErrorUserAdd
	}
	row, err := result.RowsAffected()
	if err != nil {
		return message.ErrorGetRowsAffected
	}
	if row == 1 {
		return message.SUCCESS
	}
	return message.ERROR
}

/*
检查邮箱数量
*/
func CheckEmailCount(email string) int {
	var count int
	sqlStr := "SELECT COUNT(user_email) FROM bk_users WHERE user_email=?"
	err := dao.DB.Get(&count, sqlStr, email)
	if err != nil {
		return message.ErrorCheckEmail
	}
	if count == 1 {
		return message.SuccessEmailExist
	}
	if count == 0 {
		return message.SuccessEmailNotExist
	}
	return message.ERROR
}

/*
检查手机号数量
*/
func CheckPhoneCount(phone string) int {
	var count int
	sqlStr := "SELECT COUNT(user_phone) FROM bk_users WHERE user_phone=?"
	err := dao.DB.Get(&count, sqlStr, phone)
	if err != nil {
		return message.ErrorCheckPhone
	}
	if count == 1 {
		return message.SuccessPhoneExist
	}
	if count == 0 {
		return message.SuccessPhoneNotExist
	}
	return message.ERROR
}

/*
检查昵称数量
*/
func CheckNickCount(nick string) int {
	var count int
	sqlStr := "SELECT COUNT(user_nick) FROM bk_users WHERE user_nick=?"
	err := dao.DB.Get(&count, sqlStr, nick)
	if err != nil {
		return message.ErrorCheckNick
	}
	if count == 1 {
		return message.SuccessNickExist
	}
	if count == 0 {
		return message.SuccessNickNotExist
	}
	return message.ERROR
}

/*
根据UUID完善指定用户信息
*/
func PerfectUserInfoByUUID(user *User) int {
	sqlStr := "UPDATE bk_users SET user_name=?,user_update_stamp=?,user_birthday=?,user_sex=?,user_phone=?,user_department=?," +
		"user_job=? WHERE user_uuid=?"
	result, err := dao.DB.Exec(sqlStr, user.Name, user.UpdateStamp, user.Birthday, user.Sex, user.Phone, user.Department, user.Job, user.UUID)
	if err != nil {
		return message.ErrorUserPerfect
	}
	row, err := result.RowsAffected()
	if err != nil {
		return message.ErrorGetRowsAffected
	}
	if row == 1 {
		return message.SUCCESS
	}
	return message.ERROR
}

/*
根据UUID检查用户完善信息数量
*/
func CheckPerfectUserCount(user *User) {

}

/*
根据UUID获取用户角色
*/
func GetUserRoleByUUID(uuid string) (string, int) {
	var userRole string
	sqlStr := "SELECT user_role FROM bk_users WHERE user_uuid=?"
	err := dao.DB.Get(&userRole, sqlStr, uuid)
	if err != nil {
		return userRole, message.ErrorGetRole
	}
	if userRole != "" {
		return userRole, message.SUCCESS
	}
	return userRole, message.ERROR
}

/*
根据邮箱修改用户密码
*/
func UpdatePasswordByEmail(user *User) int {
	sqlStr := "UPDATE bk_users SET user_password=?,user_update_stamp=? WHERE user_email=?"
	result, err := dao.DB.Exec(sqlStr, user.Password, user.UpdateStamp, user.Email)
	if err != nil {
		return message.ErrorUpdatePassword
	}
	row, err := result.RowsAffected()
	if err != nil {
		return message.ErrorGetRowsAffected
	}
	if row == 1 {
		return message.SUCCESS
	}
	return message.ERROR

}

/*
修改旧密码
*/
func UpdatePasswordByOld(newPassword string, user *User) int {
	sqlStr := "UPDATE bk_users SET user_password=? WHERE user_email=? AND user_password=?"
	result, err := dao.DB.Exec(sqlStr, newPassword, user.Email, user.Password)
	if err != nil {
		return message.ErrorUpdatePassword
	}
	row, err := result.RowsAffected()
	if err != nil {
		return message.ErrorGetRowsAffected
	}
	if row == 1 {
		return message.SUCCESS
	}
	return message.ERROR
}

/*
根据邮箱号和密码获取uuid
*/
func GetUserInfoByEmailAndPassword(email, password string) (string, int) {
	var uuid string
	sqlStr := "SELECT user_uuid FROM bk_users WHERE user_email=? AND user_password=?"
	err := dao.DB.Get(&uuid, sqlStr, email, password)
	if err != nil {
		return uuid, message.ErrorUserLogin
	}
	if uuid != "" {
		return uuid, message.SUCCESS
	}
	return uuid, message.ERROR
}

/*
根据手机号和密码获取uuid
*/
func GetUserInfoByPhoneAndPassword(phone, password string) (string, int) {
	var uuid string
	sqlStr := "SELECT user_uuid FROM bk_users WHERE user_phone=? AND user_password=?"
	err := dao.DB.Get(&uuid, sqlStr, phone, password)
	if err != nil {
		return uuid, message.ErrorUserLogin
	}
	if uuid != "" {
		return uuid, message.SUCCESS
	}
	return uuid, message.ERROR
}

/*
根据昵称或邮箱号和密码获取UUID
*/
func GetUserUUIDB(account, password string) (string, int) {
	var uuid string
	sqlStr := "SELECT user_uuid FROM bk_users WHERE (user_nick=? OR user_email=?) AND user_password=?"
	err := dao.DB.Get(&uuid, sqlStr, account, account, password)
	if err != nil {
		return "", message.ErrorUserLogin
	}
	if uuid == "" {
		return "", message.ErrorUUIDNotExist
	}
	return uuid, message.SUCCESS
}

/*
根据UUID获取单个用户信息
*/
func GetUserInfoByUUID(uuid string) (User, int) {
	var user User
	sqlStr := " SELECT user_create_stamp,user_update_stamp,user_nick,user_name,user_birthday,user_sex,user_phone,user_email," +
		"user_department,user_job,user_role FROM bk_users WHERE user_uuid=?"
	err := dao.DB.Get(&user, sqlStr, uuid)
	if err != nil {
		return user, message.ErrorGetUserInfo
	}
	return user, message.SUCCESS
}

/*
获取用户列表区间
page 第几页
line 多少行
*/
func GetListUserInfoTen(page, line int) ([]User, int) {
	var user []User
	sqlStr := "SELECT user_create_stamp,user_update_stamp,user_nick,user_name,user_birthday,user_sex,user_phone,user_email," +
		"user_department,user_job,user_role FROM bk_users LIMIT ?,?"
	err := dao.DB.Select(&user, sqlStr, (page-1)*line, line)
	if err != nil {
		return nil, message.ErrorGetUserInfo
	}
	return user, message.SUCCESS
}
